﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GestionWeb.Models;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using RESTServicesMov.Excepciones;
using RESTServicesMov.Dominio;

namespace GestionWeb.Controllers
{
    public class AlmacenProdController : Controller
    {



        private List<AlmacenProd> CrearAlmacenProd()
        {
        
            HttpWebRequest req = (HttpWebRequest)WebRequest.
            Create("http://localhost:50310/Movimientos.svc/CentroProdConsulta");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string detalleJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<AlmacenProd> consLista = js.Deserialize<List<AlmacenProd>>(detalleJson);

            return consLista; 
        }

        private AlmacenProd ObtenerAlmacenProd(int Codigo)
        {
            //List<AlmacenProd> AlmacenProds = (List<AlmacenProd>)Session["AlmacenProd"];
            List<AlmacenProd> AlmacenProdss = (List<AlmacenProd>)Session["AlmacenProds"];
            AlmacenProd model = AlmacenProdss.Single(delegate(AlmacenProd AlmacenProd)
            {
                //if (AlmacenProd.Codigo == Codigo) return true;
                //else 
                return false;
            });
            return model;
        } 
         
        public ActionResult Index()
        {
            //if (Session["AlmacenProds"] == null)
            //    Session["AlmacenProds"] = CrearAlmacenProd();
            //List<AlmacenProd> model = (List<AlmacenProd>)Session["AlmacenProds"];
            //return View(model);

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.
         Create("http://localhost:50310/Movimientos.svc/CentroProdConsulta");
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string detalleJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<CentroProd> consLista = js.Deserialize<List<CentroProd>>(detalleJson);
                 
                Session["consulta"] = consLista;
                 List<CentroProd> model = (List<CentroProd>)Session["consulta"];
                 return View(model);
                //return View("Index", model);
            } 

                  catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Excepcion Ex = js.Deserialize<Excepcion>(error);

                //ModelState.AddModelError(String.Empty, e.Message);
                ModelState.AddModelError(String.Empty, Ex.Mensaje);
                return View();
                    }
              
        }

        //
        // GET: /AlmacenProd/Details/5

        public ActionResult Details(int id)
        {
            AlmacenProd model = ObtenerAlmacenProd(id);
            return View(model);
        }

        //
        // GET: /AlmacenProd/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AlmacenProd/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                int idproducto = int.Parse(collection["idProducto"]);
                int idcentro = int.Parse(collection["idCentro"]);
                int Stock = int.Parse(collection["Stock"]);
                int StockMin = int.Parse(collection["StockMin"]);
                int StockMax = int.Parse(collection["StockMax"]);
                //Session["consulta"] = ObtenerDatos(id);
                //List<ConsultaCS> model = (List<ConsultaCS>)Session["consulta"];
                //return View("Reporte", model);
                //return View("Reporte", model);
                HttpWebRequest req = (HttpWebRequest)WebRequest.
         Create("http://localhost:50310/ConsultasCSs.svc/CentroProdCrear/" + idcentro + "," + idproducto + "," + Stock + "," + StockMin + "," + StockMax);
                req.Method = "POST";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string detalleJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<CentroProd> consLista = js.Deserialize<List<CentroProd>>(detalleJson);

                Session["consulta"] = Index();
                List<CentroProd> model = (List<CentroProd>)Session["consulta"];
                return View("Reporte", model);
            }



            catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Excepcion Ex = js.Deserialize<Excepcion>(error);

                //ModelState.AddModelError(String.Empty, e.Message);
                ModelState.AddModelError(String.Empty, Ex.Mensaje);
                return View();
            }
        }

        //
        // GET: /AlmacenProd/Edit/5

        public ActionResult Edit(int id)
        {
            AlmacenProd model = ObtenerAlmacenProd(id);

            return View(model);
        }

        //
        // POST: /AlmacenProd/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                AlmacenProd model = ObtenerAlmacenProd(id);
                model.Stock = int.Parse(collection["Stock"]);
                model.StockMax = int.Parse(collection["StockMax"]);
                model.StockMin = int.Parse(collection["StockMin"]);
                //model.Producto = collection["Producto"];

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AlmacenProd/Delete/5

        public ActionResult Delete(int id)
        {

            AlmacenProd model = ObtenerAlmacenProd(id);

            return View(model);
        }

        //
        // POST: /AlmacenProd/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                List<AlmacenProd> AlmacenProds = (List<AlmacenProd>)Session["AlmacenProds"];
                AlmacenProds.Remove(ObtenerAlmacenProd(id));

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }



       
    }
}
